package model;

import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Student
 * Date: 29/11/12
 * Time: 16:39
 * To change this template use File | Settings | File Templates.
 */
public class WorkInProgress implements TaskList {

    private LinkedList<Task> workInProgressTaskList;
    private LinkedList<Task> finishedTaskList;

    public WorkInProgress() {
        workInProgressTaskList = new LinkedList<Task>();
        finishedTaskList = new LinkedList<Task>();
    }

    public void addTask(Task backlogTask) {
        backlogTask.incrementUnitsOfWorkCompleted();
        workInProgressTaskList.add(backlogTask);
    }

    public Task removeTask() {
        return workInProgressTaskList.removeFirst();
    }

    public int numberOfTasks() {
        return this.workInProgressTaskList.size();
    }

    @Override
    public LinkedList<Task> getTaskList() {
        return workInProgressTaskList;
    }

    public List<List<Task>> getWorkTasksInProgress() {
        taskFinished();
        List<List<Task>> gameState = new ArrayList<List<Task>>();
        List<Task> column1 = new ArrayList<Task>();
        List<Task> column2 = new ArrayList<Task>();
        List<Task> column3 = new ArrayList<Task>();
        List<Task> column4 = new ArrayList<Task>();
        List<Task> column5 = new ArrayList<Task>();

        for (int i = 0; i < workInProgressTaskList.size(); i++) {
            Task task = workInProgressTaskList.get(i);

            if (task.unitsOfWorkCompleted() == 1) {
                column1.add(task);
            } else if (task.unitsOfWorkCompleted() == 2) {
                column2.add(task);
            } else if (task.unitsOfWorkCompleted() == 3) {
                column3.add(task);
            } else if (task.unitsOfWorkCompleted() == 4) {
                column4.add(task);
            } else if (task.unitsOfWorkCompleted() == 5) {
                column5.add(task);
            }
        }

        gameState.add(column1);
        gameState.add(column2);
        gameState.add(column3);
        gameState.add(column4);
        gameState.add(column5);

        return gameState;
    }

    public LinkedList<Task> getFinishedTaskList() {
        taskFinished();
        return finishedTaskList;
    }

    private void taskFinished() {
        for(int i = workInProgressTaskList.size() - 1; i >= 0; i-- ) {
            Task task = workInProgressTaskList.get(i);
            if (task.unitsOfWorkRemaining() == 0) {
                finishedTaskList.add(task);
                workInProgressTaskList.remove(i);
            }
        }

    }


}
